﻿@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<style>
    .layui-table-cell input {
        height: 30px;
        line-height: 30px;
    }

    /* 防止table的下拉列表被隐藏*/
    .layui-table-cell {
        overflow: visible;
    }

    .layui-table-box {
        overflow: visible;
    }

    .layui-table-body {
        overflow: visible;
    }
    /* 设置下拉框的高度与表格单元相同 */
    td .layui-form-select {
        margin-top: 0px;
        margin-left: -7px;
        margin-right: -7px;
    }

    .layui-table-view .layui-table th {
        overflow: hidden;
    }
</style>

<input style="display:none" id="tbid" value="@ViewBag.tbid">
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>

<script src="~/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="~/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        let $ = layui.$;

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysTable/TbSetUp/GetTbIndexList2?tbid=" + $("#tbid").val(),
            limits: [100],
            limit: 100,
            width: '100%',
            method: "post",
            defaultToolbar: [{ title: '刷新', layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'exports'],
            toolbar: "#toolbarTpl",
            cols: [[
                { field: "IndexId", title: "编码", width: 150, sort: true, templet: '<span>{{d.IndexId}}<span>' },
                { field: "IndexName", title: "名称", width: 180, sort: true},
                { field: "DataTypeName", title: "类型", width: 130, sort: true },
                { field: "IndexOrderNo", title: "排序", width: 80, templet: '#IndexOrderNo', sort: true },
                { field: "ColumnWith", title: "列宽", width: 80, templet: '#ColumnWith', sort: true, totalRow: true },
                { field: "BEmpty", title: "必填", width: 80, templet: '#switchBEmpty', unresize: true, sort: true },
                { field: "BLock", title: "锁定", width: 80, templet: '#switchBLock', unresize: true, sort: true, hide: true },
                { field: "BOrder", title: "排序", width: 80, templet: '#switchBOrder', unresize: true, sort: true, hide: true },
                { field: "BColumnShow2", title: "隐藏", width: 80, templet: '#switchBShow', unresize: true, sort: true, hide: true },
                { field: "ControlType", title: "控件类型", width: 140, templet: '#ControlType' },
                { field: "RuleId", title: "数据规范", width: 160, templet: '#RuleId' },
                { field: "BColumnShow2", title: "列表显示", width: 120, templet: '#switchBColumnShow2', unresize: true, sort: true },
                { field: "BSearch2", title: "快速查询", width: 120, templet: '#switchBSearch2', unresize: true, sort: true },
                { title: "操作", align: "center",  templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "set":
                    foreachGridTable();
                    break;
                case "refresh":
                    refresh();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.IndexId);//field Id 和 数据库表字段 Id 要一致
                    break;
            }
        });

        function edit(IndexId) {
            exLayer.open("编辑指标", "/SysTable/TbSetUp/UpIndex1?tbid=" + $("#tbid").val() + "&indexid=" + IndexId, '100%', '100%', '0px', '0px', null, null);
        }

        function refresh() {
            table.reload('tableId', {})
        }

        function add() {
            exLayer.open("添加/移除指标", "/SysTable/TbSetUp/UpIndex2?tbid=" + $("#tbid").val(), '100%', '100%', '0px', '0px', null, null);
        }

        function foreachGridTable() {
            var _list = [];
            var trList = $(".layui-table").find("tr");  //获取table下的所有tr
            for (var i = 0; i < trList.length; i++) {  //遍历所有的tr
                var tdArr = trList.eq(i).find("td"); //获取该tr下的所有td
                var _indexid = tdArr.eq(0).text();
                var column2 = tdArr.eq(1).text();
                var column3 = tdArr.eq(2).text();
                var _no = tdArr.eq(3).find('input').val();
                var _with = tdArr.eq(4).find('input').val();
                if (_indexid != '') {
                    var _row = { TbId: $("#tbid").val(), IndexId: '', ColumnWith: 0, IndexOrderNo: 0 };
                    _row.TbId = $("#tbid").val();
                    _row.IndexId = _indexid;
                    _row.ColumnWith = _with;
                    _row.IndexOrderNo = _no;
                    _list.push(_row);
                }
            }
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexWidthAndNo", "post", { "list": _list }, true).done(function (response) {
                exUtils.tableSuccessMsg(response.message);
                console.log(_list);
                location.reload(); // 页面刷新
            }).fail(function (error) {
                console.log(error);
            });
        }

        //监听切换按钮
        form.on('switch(stateBEmpty)', function (data) {
            var name = 'BEmpty';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    location.reload(); // 页面刷新
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //监听切换按钮
        form.on('switch(stateBOrder)', function (data) {
            var name = 'BOrder';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    location.reload(); // 页面刷新
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //监听切换按钮
        form.on('switch(stateBLock)', function (data) {
            var name = 'BLock';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //监听切换按钮
        form.on('switch(stateBColumnShow2)', function (data) {
            var name = 'BColumnShow2';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //监听切换按钮
        form.on('switch(stateBColumnShow)', function (data) {
            var name = 'BColumnShow';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //监听切换按钮
        form.on('switch(stateBSearch2)', function (data) {
            var name = 'BSearch2';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //监听切换按钮
        form.on('switch(stateBShow)', function (data) {
            var name = 'BShow';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //监听切换按钮
        form.on('switch(stateBSearch)', function (data) {
            var name = 'BSearch';
            var indexid = this.value;
            var isState = data.elem.checked;
            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexSome?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&isState=" + isState + "&name=" + name, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //数据规范
        form.on('select(RuleId)', function (data) {
            var ruleid = data.value;
            var indexid = data.elem.getAttribute("name").replace(/rule_/, "");

            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexRule?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&ruleid=" + ruleid, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //控件类型
        form.on('select(ControlType)', function (data) {
            var typeid = data.value;
            var indexid = data.elem.getAttribute("name").replace(/type_/, "");

            exUtils.ajax("/SysTable/TbSetUp/UpTbIndexControlType?tbid=" + $("#tbid").val() + "&indexid=" + indexid + "&typeid=" + typeid, "post", null, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });
    })</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="set" id="set"><i class="layui-icon layui-icon-list"></i>保存设置</button>
    <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="add" id="add"><i class="layui-icon layui-icon-addition"></i>添加/移除指标</button>
</script>
<!-- 控件类型 -->
<script type='text/html' id='ControlType'>
    <div class='layui-input-inline' style='width:100px;'>
        <select name={{'type_'+d.IndexId}} lay-filter='ControlType'>
            {{d.ControlType_Exa}}
        </select>
    </div>
</script>
<!-- 数据规范 -->
<script type='text/html' id='RuleId'>
    <div class='layui-input-inline' style='width:120px;'>
        <select name={{'rule_'+d.IndexId}} lay-filter='RuleId' data-value={{d.RuleId}}>
            <option value="">==请选择==</option>
            {{d.Rule_Exa}}
        </select>
    </div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" id="edit">编辑</a>
</script>
<!-- 必填 -->
<script type="text/html" id="switchBEmpty">
    <span title="必填"><input type="checkbox" name="BEmpty" value="{{d.IndexId}}" lay-skin="switch" lay-text="必填|" lay-filter="stateBEmpty" {{ d.BEmpty == 2 ? 'checked' : '' }}></span>
</script>
<!-- 排序 -->
<script type="text/html" id="switchBOrder">
    <span title="排序"><input type="checkbox" name="BOrder" value="{{d.IndexId}}" lay-skin="switch" lay-text="排序|" lay-filter="stateBOrder" {{ d.BOrder == 1 ? 'checked' : '' }}></span>
</script>
<!-- 是否隐藏？1显示2不显示 -->
<script type="text/html" id="switchBShow">
    <span title="查询列表中该列显示"><input type="checkbox" name="BColumnShow" value="{{d.IndexId}}" lay-skin="switch" lay-text="隐藏|" lay-filter="stateBShow" {{ d.BShow == 2 ? 'checked' : '' }}></span>
</script>
<!-- 是否锁定？1是2否 -->
<script type="text/html" id="switchBLock">
    <span title="锁定"><input type="checkbox" name="BLock" value="{{d.IndexId}}" lay-skin="switch" lay-text="锁定|" lay-filter="stateBLock" {{ d.BLock == 1 ? 'checked' : '' }}></span>
</script>
<!-- 列表显示(list)？1显示2不显示 -->
<script type="text/html" id="switchBColumnShow2">
    <span title="列表显示"><input type="checkbox" name="BColumnShow2" value="{{d.IndexId}}" lay-skin="switch" lay-text="显示|" lay-filter="stateBColumnShow2" {{ d.BColumnShow2 == 1 ? 'checked' : '' }}></span>
</script>
<!-- 查询列表中该列显示？1显示2不显示 -->
<script type="text/html" id="switchBColumnShow">
    <span title="查询列表中该列显示"><input type="checkbox" name="BColumnShow" value="{{d.IndexId}}" lay-skin="switch" lay-text="显示|" lay-filter="stateBColumnShow" {{ d.BColumnShow == 1 ? 'checked' : '' }}></span>
</script>
<!-- 是否做为查询条件？1是2不是 -->
<script type="text/html" id="switchBSearch">
    <span title="是否做为查询条件"><input type="checkbox" name="BSearch" value="{{d.IndexId}}" lay-skin="switch" lay-text="是|否" lay-filter="stateBSearch" {{ d.BSearch == 1 ? 'checked' : '' }}></span>
</script>
<!-- 是否做为快速查询条件？1是2不是 -->
<script type="text/html" id="switchBSearch2">
    <span title="是否做为快速查询条件"><input type="checkbox" name="BSearch2" value="{{d.IndexId}}" lay-skin="switch" lay-text="是|否" lay-filter="stateBSearch2" {{ d.BSearch2 == 1 ? 'checked' : '' }}></span>
</script>
<!-- 列宽 -->
<script type="text/html" id="ColumnWith">
    <input type="text" name="ColumnWith" value="{{d.ColumnWith}}" class="layui-input" maxlength="4" style="height:28px;" lay-verify="required|number">
</script>
<!-- 排序号 -->
<script type="text/html" id="IndexOrderNo">
    <input type="text" name="IndexOrderNo" value="{{d.IndexOrderNo}}" class="layui-input" maxlength="2" style="height:28px;" lay-verify="required|number">
</script>